package vn.allmobile.ambnews.fragment;

import java.util.ArrayList;
import java.util.List;

import vn.allmobile.ambnews.R;
import vn.allmobile.ambnews.activities.list.AllPapersAdapter;
import vn.allmobile.ambnews.activities.main.MainAllPapersActivity;
import vn.allmobile.ambnews.data.CacheFile;
import vn.allmobile.ambnews.data.Category;
import vn.allmobile.ambnews.data.ListPaperData;
import vn.allmobile.ambnews.data.Paper;
import vn.allmobile.ambnews.network.NetworkAPI;
import vn.allmobile.ambnews.network.NetworkChangeReceiver;
import vn.allmobile.ambnews.network.SynchronizeService;
import vn.allmobile.ambnews.utils.Contants;
import vn.allmobile.ambnews.utils.GlobalApplication;
import vn.allmobile.ambnews.utils.Utils;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.aphidmobile.utils.IO;

@SuppressLint("NewApi")
public class allpapers_treeview_fragment extends Fragment {
	private ExpandableListView mExpandableListView;
	private List<Paper> mListPapers;
	AllPapersAdapter mAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View mView = (LinearLayout) inflater.inflate(
				R.layout.activity_allpapers_treeview, container, false);
		mExpandableListView = (ExpandableListView) mView
				.findViewById(R.id.allpapers_expandableListView);
		//
		// Load file and load data
		/*String mCachedFile = CacheFile.readFile(getActivity(),
				Contants.CACHE_ALLPAPERS_LIST, Contants.CACHE_PATH);
		List<Paper> mTempList = new ArrayList<Paper>();
		if (mCachedFile != null) {
			ListPaperData list = (ListPaperData) IO.convertStringToObject(
					mCachedFile, ListPaperData.class);
			mTempList = list.getListPaper();
			for (Paper paper : mTempList) {
				for (Paper paper2 : this.mListPapers) {
					if (paper.getId().equals(paper2.getId())) {
						paper2.setEnable(paper.isEnable());
					}
				}
			}
		}
		*/
		for(Paper upp : NetworkAPI._userListPapers)
		{
			for(Paper pp : NetworkAPI._papers)
			{
				if(upp.getId().equals(pp.getId()))
				{
					pp.setEnable(true);
					for(Category ucat : upp.getCategories())
					{
						for(Category cat : pp.getCategories())
						{
							if(ucat.getId().equals(cat.getId()))
							{
								cat.setEnable(true);
							}
						}
					}
				}
			}
		}
		
		this.mListPapers = NetworkAPI._()._papers;
		
		mAdapter = new AllPapersAdapter(getActivity(), this.mListPapers);
		//
		mExpandableListView.setAdapter(mAdapter);
		// Button save
		Button mSaveButton = (Button) mView
				.findViewById(R.id.allpapers_savebutton);
		mSaveButton.setOnClickListener(saveSettings);
		// Button close
		Button mCloseButton = (Button) mView
				.findViewById(R.id.allpapers_closebutton);
		mCloseButton.setOnClickListener(closeScreen);

		return mView;
	}

	private OnClickListener saveSettings = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// NetworkAPI._()._papers = mAdapter.getPapers();
			List<Paper> mList = new ArrayList<Paper>();
			
			for (Paper paper : mAdapter.getPapers()) {
				if (paper.isEnable()) {
					List<Category> categories = new ArrayList<Category>();
					for(Category cat : paper.getCategories())
					{
						if(cat.isEnable() == true)
						{
							categories.add(cat);
						}
					}
					Paper tmp = new Paper(paper.getId(), paper.getTitle(), paper.getImage(), categories);
					mList.add(tmp);
				}
			}
			if(mList.size()>0)
			{				
				NetworkAPI._userListPapers = mList;
				NetworkAPI._()._readySync =  true;				
				SynchronizeService.nextTimeStart();
			}
			
			// Save to file
			CacheFile.saveFile(getActivity(), Contants.CACHE_ALLPAPERS_LIST,
					Contants.CACHE_PATH,
					IO.convertObjectToString(new ListPaperData(mList)));
			Toast.makeText(getActivity(),"Đã lưu." , Toast.LENGTH_SHORT).show();	
			v.setEnabled(false);
			//
			Activity mAc = getActivity();
			if (mAc instanceof MainAllPapersActivity) {
				((MainAllPapersActivity) mAc).datachanged();
			}
			
	    	if(NetworkChangeReceiver.isConnected(true))
	    	{
	    		GlobalApplication.startService();
	    	}
		}
	};

	private OnClickListener closeScreen = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Activity mAc = getActivity();
			Intent intent = new Intent();
			mAc.setResult(Activity.RESULT_OK, intent);
			mAc.finish();
		}
	};
}
